<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <TodoHeader @addList="addList" />

    <!-- 主体部分 -->
    <TodoBody
      :list="renderList"
      @delList="delList"
      @changeStatus="changeStatus"
    />

    <!-- 底部部分 -->
    <TodoFooter
      :left="left"
      @clear="clear"
      :isDoneAll="isDoneAll"
      :type="type"
      @changeType="changeType"
      v-show="list.length"
    />
  </section>
</template>

<script>
import TodoHeader from '@/components/TodoHeader.vue';
import TodoBody from '@/components/TodoBody.vue';
import TodoFooter from '@/components/TodoFooter.vue';
export default {
  components: {
    TodoHeader,
    TodoBody,
    TodoFooter,
  },
  data() {
    return {
      type: 'all',
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true },
      ],
    };
  },
  methods: {
    delList(id) {
      console.log(id);
      // this.list.splice(id-1,1)
      this.list = this.list.filter((item) => item.id !== id);
    },
    changeStatus(id) {
      console.log(id);
      const res = this.list.find((item) => item.id === id);
      res.isDone = !res.isDone;
    },
    addList(caseName) {
      console.log(caseName);
      const obj = { id: +new Date(), name: caseName, isDone: false };
      this.list = [obj, ...this.list];
    },
    clear() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    changeType(type) {
      this.type = type;
    },
  },
  computed: {
    left() {
      return this.list.filter((item) => !item.isDone).length;
    },
    isDoneAll() {
      return this.list.some((item) => item.isDone);
    },
    renderList() {
      switch (this.type) {
        case 'all':
          return this.list;

        case 'active':
          return this.list.filter((item) => !item.isDone);
        case 'completed':
          return this.list.filter((item) => item.isDone);

        default:
          break;
      }
    },
  },
};
</script>

<style></style>
